# /usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2022/1/10 23:38
# @Author  : lt
# @Email   : lt@163.com
# @File    : test-add_kehuguanhuai.py
# @Software: PyCharm
'''CRM_KHGL_KH_135 验证添加客户关怀功能_全部合法输入'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from time import sleep
driver = webdriver.Chrome()
url = 'http://192.168.1.55/crm'
driver.get(url)
#定位用户名输入框
username_locator = (By.NAME, 'name')
#定位密码输入框
password_locator = (By.NAME, 'password')
#定位登录按钮
submit_locator = (By.NAME, 'submit')
#定位客户控件按钮
kehu_locator = (By.LINK_TEXT,'客户')
#定位客户关怀按钮
kehu_guanhuai_locator = (By.CSS_SELECTOR,'body > div.container > div.page-header > ul > li:nth-child(4) > a')
#定位新建关怀按钮
add_guanhuai_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > div > a > i')
#定位客户输入框
kehu_shuru_locator = (By.ID,'customer_name')
#定位客户表
tobdy_locator = (By.ID,'datas')
#定位确认按钮
queren_locator = (By.XPATH,'/html/body/div[8]/div[3]/div/button[1]/span')
#定位关怀主题
guanhuai_zhuti_locator = (By.ID,'name')
#定位关怀日期
guanhuai_time_locator = (By.ID,'care_time')
#定位执行人输入框
fuzeren_locator = (By.ID,'owner_name')
#定位执行人表
tobdy_fuzeren_locator = (By.ID,'d_content')
#定位执行人勾选确定按钮
queren_fuzeren_locator = (By.XPATH,'/html/body/div[7]/div[3]/div/button[1]/span')

#定位关怀类型下拉框
guanhuai_leixing_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div > form > table > tbody > tr:nth-child(4) > td:nth-child(4) > select')
#定位关怀内容输入框
guanhuai_neirong_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div > form > table > tbody > tr:nth-child(5) > td:nth-child(2) > textarea')
#定位备注输入框
description_locator = (By.XPATH,'/html/body/div[5]/div[2]/div/form/table/tbody/tr[6]/td[2]/textarea')
#定位保存按钮
baocun_locator = (By.NAME,'submit')
#定位添加成功
add_succeful_locator = (By.CSS_SELECTOR,'body > div.container > div.alert.alert-success')

#输入数据
kehu = '爱奇艺'
lianxiren = '蒙刚'
zhuti = '生日祝福'#关怀主题
guabhuai_time = '2022-01-10 23:56'
zhixingren = '李总'
neixing = '电话'
guanhuai_neirong = '送上生日祝福'
description = '关怀的时候不谈生意'
#登录信息
username, password = 'admin','admin123'
#删除的客户名
kehu_name = '爱奇艺'
#输入用户名
driver.find_element(*username_locator).send_keys(username)
#输入密码
driver.find_element(*password_locator).send_keys(password)
#点击登录按钮
driver.find_element(*submit_locator).click()
#点击客户按钮
driver.find_element(*kehu_locator).click()
#点击客户关怀
driver.find_element(*kehu_guanhuai_locator).click()
#点击新建客户关怀
driver.find_element(*add_guanhuai_locator).click()
#点击客户输入框
driver.find_element(*kehu_shuru_locator).click()
sleep(4)
#选择客户
tobdy_kehu = driver.find_element(*tobdy_locator)
trs = tobdy_kehu.find_elements(By.TAG_NAME,'tr')
for i in trs:
    tds = i.find_elements(By.TAG_NAME,'td')
    if tds[1].text == kehu:
        tds[0].find_element(By.CSS_SELECTOR,'[name="customer"]').click()
sleep(4)
#点击确认按钮
driver.find_element(*queren_locator).click()
#输入关怀主题
driver.find_element(*guanhuai_zhuti_locator).send_keys(zhuti)
#输入关怀日期
driver.find_element(*guanhuai_time_locator).send_keys(guabhuai_time)
#点击执行人输入框
driver.find_element(*fuzeren_locator).click()
#选择要输入的负责人
sleep(4)
tobdy_fuzeren = driver.find_element(*tobdy_fuzeren_locator)
trs2 = tobdy_fuzeren.find_elements(By.TAG_NAME,'tr')
for e in trs2:
    tds2 = e.find_elements(By.TAG_NAME,'td')
    if tds2[1].text == 'admin':
        #点击负责人
        tds2[0].click()
#点击确定
sleep(4)
driver.find_element(*queren_fuzeren_locator).click()
#输入关怀类型
Select(driver.find_element(*guanhuai_leixing_locator)).select_by_visible_text(neixing)
#输入关怀内容
driver.find_element(*guanhuai_neirong_locator).send_keys(guanhuai_neirong)
#输入备注
driver.find_element(*description_locator).send_keys(description)
#点击保存
driver.find_element(*baocun_locator).click()
#断言
if '成功' in driver.find_element(*add_succeful_locator).text:
    print('pass')
else:
    raise AssertionError('测试不通过')
